<template>
	<view style="padding: 60upx;background-color: #EBEBEB;">
		<input :min="6" :max="30" class="input" v-model="username" placeholder="请输入手机号或账号"></input>
		<input :min="6" :max="30" type="password" class="input" v-model="password" placeholder="请输入密码"></input>
		<!-- <input :min="4" :max="30" class="username" :value="verifycode" v-model="verifycode" placeholder="请输入验证码"></input> -->

		<view style="display: flex;justify-content: space-between;">
			<input :min="4" class="input" v-model="captcha" placeholder="请输入验证码" style="width: 170px;"></input>
			<img :src="captchaPath" height="38" @click="getCaptcha()" alt="" style='margin-top: 2px;'>
		</view>
		<view style="display: flex;justify-content: space-between;font-size: 30upx;line-height: 80upx;">
			<text @click="register()">用户注册</text>
			<text @click="forgetPass()">忘记密码</text>
		</view>
		<button type="warn" style="margin-top: 30upx;" @click="login">登录</button>
	</view>
</template>
<script>
	import {
		get,
		post,
		showToast
	} from '../../js/common';
	export default {
		data() {
			return {
				captchaPath: '',
				username: '',
				password: '',
				uuid: '',
				captcha: '',
				mark:''
			}
		},
		onLoad(options) {
			this.mark = options.mark?options.mark:''
			this.getCaptcha()
		},
		methods: {
			getUUID() {
				return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
					return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
				})
			},
			// 获取验证码
			getCaptcha() {
				this.uuid = this.getUUID()
				 this.captchaPath = 'http://39.105.185.57:9002/mobileapi/mobile/captcha.jpg?uuid=' + this.uuid
				// this.captchaPath = 'http://192.168.10.149:9002/mobileapi/mobile/captcha.jpg?uuid=' + this.uuid
				// this.captchaPath = 'http://192.168.10.43:9002/mobileapi/mobile/captcha.jpg?uuid=' + this.uuid
			},
			login() {
				const params = {
					username: this.username,
					password: this.password,
					captcha: this.captcha,
					uuid: this.uuid
				}
				post('mobileapi/mobile/login', params).then(res => {
					
					if (res.code === 0) {
						uni.setStorageSync('token', res.token)
						uni.setStorageSync('userId', res.userId)
						if(this.mark){
							uni.switchTab({
								url:'../me/index'
							})
						}else{
							uni.navigateBack(1)
						}
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						this.getCaptcha()
					}
				})
			},
			register() {
				uni.navigateTo({
					url: '/pages/register/index'
				})
			},
			forgetPass() {
				uni.navigateTo({
					url: '/pages/forgetPass/index'
				})
			},
		},
	}
</script>
<style>
</style>
